<?php
// +----------------------------------------------------------------------
// | Copyright (c) 2018-2018 http://www.donglixia.net All rights reserved.
// +----------------------------------------------------------------------
// | Author: 十万马 <962863675@qq.com>
// +----------------------------------------------------------------------
// | DateTime: 2018-02-09 16:17
// +----------------------------------------------------------------------

namespace app\english\controller;

use think\Db;
use think\Request;

class Search extends Base
{
    /**
     * 默认需要获取的字段
     * @var string
     */
    private $language = 1;
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        if ($this->request->module() == 'english') {
            $this->language = 2;
        } elseif ($this->request->module() == 'tradi') {
            $this->language = 3;
        }
    }

    /**
     * 搜索页
     * @return mixed
     */
    public function search()
    {
        //获取数据列表
        $param = $this->request->param();
        $cook_sea = cookie('search_ele');
        $limit = !empty($param['limit']) ? $param['limit'] : 20 ;
        if (!empty($param['keywords'])) {
            $keywords = trim($param['keywords']);
        } elseif(!empty($cook_sea['keywords'])) {
            $keywords = $cook_sea['keywords'];
        } else {
            $keywords = '';
        }

        //搜索条件
        $search_ele = [];
        $search_ele['keywords'] = $keywords;
        $search_ele['type'] = !empty($param['type']) ? $param['type'] : 'goods';
        $search_ele['page_goods'] = !empty($param['page_goods']) ? $param['page_goods'] : 0;
        $search_ele['page_article'] = !empty($param['page_article']) ? $param['page_article'] : 0;
        cookie('search_ele', $search_ele);
        $this->assign('search_ele', $search_ele);//关键词

        //获取产品数据，分页
        $param_goods = ['keywords'=>$keywords, 'type' =>'goods'];
        $list_goods = Db::name('goods')
            ->where('goods_name|short_name|area|food_mix|goods_style|wine_label|keywords|describe|detail|area_detail', 'like', '%'.$keywords.'%')
            ->where('cat_id', '<>', 71)
            ->where('language', $this->language)
            ->field('goods_id,`goods_name`,`short_name`,years,area,img_url_thumb')
            ->order('create_time desc, modify_time desc')
            ->paginate($limit, false, ['query'=>$param_goods, 'var_page' =>'page_goods'])
            ->each(function ($item, $key)use ($keywords){
                $item['years'] = Db::name('goods_cat')->where('cat_id', $item['years'])->value('cat_name');
                return $item;
            });
        $this->assign('list_goods', $list_goods);//数据列表，产品

        //获取文章数据，分页
        $param_art = ['keywords'=>$keywords, 'type' =>'article'];
        $list_art = Db::name('article')
            ->where('title|title_sub|keywords|describe|content|author', 'like', '%'.$keywords.'%')
            ->where('language', $this->language)
            ->field('art_id as id,`title`,`title_sub`,`keywords`,concat("article|",`cat_id`) as cat_info,`author`,`describe`,`content` ,`create_time`,`modify_time` ')
            ->order('create_time desc, modify_time desc')
            ->paginate($limit, false, ['query'=>$param_art, 'var_page' =>'page_article'])
            ->each(function ($item, $key)use ($keywords){
                return $this->formatDataArt($item, $keywords);
            });
        $this->assign('list_art', $list_art);//数据列表，文章

        //数量
        $page_num = [];
        $page_num['total'] = $list_goods->total() + $list_art->total();
        $page_num['goods'] = $list_goods->total();
        $page_num['article'] = $list_art->total();
        $this->assign('page_num', $page_num);//网站总数

        $this->assign('web', ['title' => '搜索结果']);//网站标题
        return $this->fetch();
    }

    /**
     * 格式化文章数据
     * @param $item
     * @param $keywords
     * @return mixed
     */
    private function formatDataArt($item, $keywords){
        //标题
        $title = $item['title'];
        $title .= !empty($item['title_sub'])? ' | '.$item['title_sub'] : '';
        $title .= !empty($item['keywords'])? ' | '.$item['keywords'] : '';
        $item['title'] = preg_replace('/'.$keywords.'/', '<span>'.$keywords.'</span>', $title);

        //内容，去掉html标签
        $str = strip_tags(htmlspecialchars_decode($item['author'].$item['describe'].$item['content']));
        $str = preg_replace('/'.$keywords.'/', '<span>'.$keywords.'</span>', $str);
        $item['content'] = mb_substr($str, 0, 200, 'utf-8');//截取100个中文

        //链接
        $link = '';
        $cat_info = explode('|', $item['cat_info']);
        if ($cat_info[0] == 'goods') {
            $link = url('goods/detail')."?id=".$item['id'];
        } else {
            $cat_type = Db::name('article_cat')->where('cat_id', $cat_info[1])->value('cat_type');
            $basic_code = Db::name('basic_info')->where('basic_id', $cat_type)->value('basic_code');
            switch ($basic_code) {
                case "AD0001":
                    $link = url('news/detail')."?id=".$item['id'];//信息中心
                    break;
                case "AD0002":
                    $link = url('activity/detail')."?id=".$item['id'];//活动中心
                    break;
                case "AD0003":
                    $link = url('college/college');//葡萄酒学院
                    break;
                case "AD0004":
                    $link = url('about/aboutt');//关于我们
                    break;
                case "AD0005":
                    $link = url('brand/brand');//品牌中心
                    break;
                case "AD0006":
                    $link = url('guide/faq');//常见问题
                    break;
                case "AD0007":
                    $link = url('index/index');//首页
                    break;
                case "AD0008":
                    $link = url('fund/detail');//爱心基金
                    break;
            }
        }
        $item['link'] = $this->request->domain().$link;
        return $item;
    }



}

